import Vue from 'vue';
import Router from 'vue-router';


import Home from '@/page/home.vue'; //首页
import Sort from '@/page/sort.vue' //分类页一
import SortS from '@/page/sortS.vue' //分类页一
import Adviser from '@/page/adviser.vue' //顾问
import Cart from '@/page/cart.vue' //购物车
import MyIndex from '@/page/my.vue' //我的主页
import Login from '@/page/login.vue' //登录页
import Reg from "@/page/reg.vue" //注册页
// import My from "@/page/my.vue" //我的
import Detail from "@/page/detail.vue" //详情页
import Order from "@/page/order.vue" //订单填写
import GenOrder from '@/page/genOrder.vue' //订单生成页
import Pay from '@/page/pay.vue' //支付
import MyOrder from '@/page/myOrder.vue' //我的订单

Vue.use(Router);

const router = new Router({
	routes: [{
			path: '/', //项目初始页面，必须配置
			component: Home, //
			name: 'Home', //页面名字
			meta: { //数据源
				title: "首页"
			}
		},
		{
			path: '/home',
			component: Home,
			name: 'Home',
			meta: {
				title: "首页"
			}
		},
		{
			path: '/sort',
			component: Sort,
			name: 'Sort',
			meta: {
				title: "分类"
			}
		},
		{
			path: '/sortS',
			component: SortS,
			name: 'SortS',
			meta: {
				title: "分类"
			}
		},
		{
			path: '/adviser',
			component: Adviser,
			name: 'Adviser',
			meta: {
				title: "顾问"
			}
		},
		{
			path: '/cart',
			component: Cart,
			name: 'Cart',
			meta: {
				title: "购物车"
			}
		},
		{
			path: '/myIndex',
			component: MyIndex,
			name: 'MyIndex',
			meta: {
				needLogin: true,
				title: "个人中心"
			}
		},
		// {
		// 	path:'/my', 
		// 	component:My, 
		// 	name:'My' ,
		// 	meta: { 
		// 		title: "中心"
		// 	}
		// },
		{
			path: '/login',
			component: Login,
			name: 'Login',
			meta: {
				title: "登录"
			}
		},
		{
			path: '/reg',
			component: Reg,
			name: 'Reg',
			meta: {
				title: "注册"
			}
		},
		{
			path: '/detail',
			component: Detail,
			name: 'Detail',
			meta: {
				title: "详情页"
			}
		},
		{
			path: '/order',
			component: Order,
			name: 'Order',
			meta: {
				needLogin: true,
				title: "订单填写"
			}
		},
		{
			path: '/genOrder',
			component: GenOrder,
			name: 'GenOrder',
			meta: {
				title: "订单生成"
			}
		},
		{
			path: '/pay',
			component: Pay,
			name: 'Pay',
			meta: {
				title: "支付"
			}
		},
		{
			path: '/myOrder',
			component: MyOrder,
			name: 'MyOrder',
			meta: {
				title: "订单"
			}
		}
	]
})


/* 添加路由拦截 */
router.beforeEach((to, from, next) => {
	// console.log(to, from);

	//从本地存储中获取cookie中获取token（用户登录信息—）
	let token = localStorage.getItem("token"); //能取到则有登录
	// console.log(to.meta.needLogin)
	if (to.meta.needLogin && !token) { //用户从未登录过
		//跳转登录页面
		localStorage.setItem("toFullPath", to.fullPath) //登录完成后跳转的的页面
		next('/login')

	} else {
		document.title = to.meta.title;
		next(); //继续下面的操作
	}

})

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}
export default router;
